﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using ZHDJ.Data.Models.Center;
using ZHDJ.WebPage;
using ZHDJ.WebPage.Master;

namespace ZHDJ.Web.Portal.PartyRepresentative
{
    public partial class Meeting : UseMorePageMasterPage
    {
        /// <summary>
        /// 工作室ID
        /// </summary>
        private string StudioID
        {
            get
            {
                return this.Request["StudioID"] ?? "null";
            }
        }

        /// <summary>
        /// 页数
        /// </summary>
        private string PageIndex
        {
            get
            {
                return this.Request["PageIndex"] ?? "";
            }
        }
        /// <summary>
        /// 
        /// </summary>
        private string DataCount
        {
            get
            {
                return this.Request["DataCount"] ?? "";
            }
        }
        /// <summary>
        /// 每页条数
        /// </summary>
        private string PageSize
        {
            get
            {
                return this.Request["PageSize"] ?? "";
            }
        }
        /// <summary>
        /// 
        /// </summary>
        private string QueryText
        {
            get
            {
                return this.Request["QueryText"] ?? "";
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.hdStudioID.Value = StudioID;
            }

            if (!string.IsNullOrEmpty(this.PageIndex))
            {
                WriteList();
            }
            else if (!string.IsNullOrEmpty(this.DataCount))
            {
                WriteDataCount();

            }
        }

        #region 接访排期
        private void WriteList()
        {
            StringBuilder sbHtml = new StringBuilder();
            DataTable dt = PartyRepresentativeMeeting.GetGridData(Convert.ToInt32(this.PageIndex), Convert.ToInt32(PageSize), "4", "", StudioID).Tables[0];
            dt.Columns.Add("Title2", typeof(string));
            dt.Columns.Add("BeginTime2", typeof(string));
            foreach (DataRow dr in dt.Rows)
            {
               
                dr["Title2"] = dr["Title"]+ PartyRepresentativeInfo.GetPartyRepresentativeJoin(DB,Convert.ToString(dr["ID"]));
                dr["BeginTime2"] = Convert.ToDateTime(dr["BeginTime"]).ToString("yyyy-MM-dd HH:mm");
            }
            var json = JsonConvert.SerializeObject(dt);
            Response.Clear();
            Response.Write(json);
            Response.End();
        }
        private void WriteDataCount()
        {
            var dd = PartyRepresentativeMeeting.GetGridData(0, 1, "4", "", StudioID);
            Response.Clear();
            Response.Write(PartyRepresentativeMeeting.GetGridData(0, 1, "4",  "", StudioID).Tables[1].Rows[0][0].ToString());
            Response.End();
        }
        #endregion

        #region 本周接访

        public string LoadWeekHmtl()
        {
            StringBuilder html = new StringBuilder();
            StringBuilder sbHead = new StringBuilder();
            sbHead.Append(" <tr class='tr_b'>");
            sbHead.Append("      <th rowspan='{0}' style='width:200px' class='th_b'>");
            sbHead.Append("      {1}");
            sbHead.Append("      </th>");
            sbHead.Append("      <td class='td_b'>");
            sbHead.Append("        <span style=\"float: right;width:100px; margin-right: 20px;text-align:right\">{2}</span>");
            sbHead.Append("        <a style=\"float: left\" href='MeetingDetail.aspx?id={3}' target=\"_blank\">{4}</a>");
            sbHead.Append("      </td>");
            sbHead.Append(" </tr>");

            StringBuilder sbContent = new StringBuilder();
            sbContent.Append(" <tr class='tr_b'>");
            sbContent.Append("      <td class='td_b'>");
            sbContent.Append("        <span style=\"float: right;width:100px; margin-right: 20px;text-align:right\">{0}</span>");
            sbContent.Append("        <a style=\"float: left\" href='MeetingDetail.aspx?id={1}' target=\"_blank\">{2}</a>");
            sbContent.Append("    </td>");
            sbContent.Append(" </tr>");

            int week = (int)DateTime.Now.DayOfWeek;
            for (int i = 1; i <= 7; i++)//一周七天
            {
                DateTime dt = DateTime.Now.AddDays(-week + i);
                string startTime = DateTime.Now.AddDays(-week + i).ToString("yyyy-MM-dd 00:00");
                string endTime = DateTime.Now.AddDays(-week + i + 1).Date.AddSeconds(-1).ToString("yyyy-MM-dd 23:59");
                var q =
                    DB.PartyRepresentativeMeetings.Where(
                        a => a.Status == 4 && a.StudioID == StudioID)
                        .ToList()
                        .Where(a => Convert.ToDateTime(a.BeginTime) >= Convert.ToDateTime(startTime) &&
                                    Convert.ToDateTime(a.BeginTime) <= Convert.ToDateTime(endTime)).OrderBy(a=>a.BeginTime);
                if (!q.Any())
                {
                    html.AppendFormat(@"<tr class='tr_b '>
                                    <th style='width:200px' class='th_b'>{0}</th>
                                    <td class='td_b'>无</td>
                                  </tr>", this.GetWeekName(i)+"（" + dt.ToString("yyyy-MM-dd")+"）  "  );
                    continue;
                }
                var first = q.FirstOrDefault();
                html.Append(string.Format(sbHead.ToString(),  q.Count(), this.GetWeekName(i)+"（"+Convert.ToDateTime(first.BeginTime).ToString("yyyy-MM-dd") + "）" ,
                    Convert.ToDateTime(first.BeginTime).ToString("HH:mm"), first.ID, first.Title+ PartyRepresentativeInfo.GetPartyRepresentativeJoin(DB, first.ID)));
                if (q.Count() <= 1) continue;
                //一天多个活动公告
                var index = 0;
                foreach (var item in q)
                {
                    if (index == 0)
                    {
                        index += 1;
                        continue;
                    }
                    html.Append(string.Format(sbContent.ToString(), Convert.ToDateTime(item.BeginTime).ToString("HH:mm"), 
                        item.ID, item.Title + PartyRepresentativeInfo.GetPartyRepresentativeJoin(DB, item.ID)));
                }
            }
            return html.ToString(); ;
        }

        private string GetWeekName(int type)
        {
            switch (type)
            {
                case 0: return "星期日";
                case 1: return "星期一";
                case 2: return "星期二";
                case 3: return "星期三";
                case 4: return "星期四";
                case 5: return "星期五";
                case 6: return "星期六";
                case 7: return "星期日";
            }
            return "";
        }
        #endregion

        #region 模块菜单
        /// <summary>
        /// 初始化module  如果通过url传参可不设置
        /// </summary>
        protected override void InitModule()
        {
            Category = "PartyRepresentativeMeeting";

        }
        #endregion
    }
}